package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Author Pan Rundong
 * @Date 2023-02-24 19:02
 * @Desc 用户控制器
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 发送验证码
     * @param map
     * @return void
     * @Author: Pan Rundong
     * @Date: 2023-02-24 19:20
     * @Greet2u: （O｀ 3′O）~~♡
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String, String> map) {
        //1.获得手机号
        String phone = map.get("phone");
        //2.调用service方法
        userService.sendCode(phone);
        //3.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 验证码登录
     *      成功：返还Token以及isNew
     *      失败：抛出登录异常
     * @Author: Pan Rundong
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map) {
        //1.获取参数
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        //2.调用service方法
        Map<String, Object> userMap = userService.verification(phone, verificationCode);
        //3.返回
        return ResponseEntity.ok(userMap);
    }

    /**
     * 保存用户详细信息
     * @Author: Pan Rundong
     * @param userInfo
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo) {
        //1.调用service方法
        userInfoService.save(userInfo);
        //2.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 检查图片是否为人脸
     *      成功：修改用户Info信息
     *      失败：抛出非人脸图片异常
     * @Author: Pan Rundong
     * @param multipartFile
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity saveHeadImage(@RequestParam("headPhoto") MultipartFile multipartFile) throws IOException {
        //1.调用service方法
        userInfoService.saveImage(multipartFile);
        //2.返回
        return ResponseEntity.ok(null);
    }

}
